package cn.edu.qfnu.common.service.Impl;

import cn.edu.qfnu.common.dao.BaseDao;
import cn.edu.qfnu.common.paginate.Pagination;
import cn.edu.qfnu.common.service.BaseService;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID>{

    @Autowired
    private BaseDao<T, ID> baseDao;

    private Class<T> clazz;

    @SuppressWarnings("unchecked")
    public BaseServiceImpl(){
        Type t = this.getClass().getGenericSuperclass();
        if (t instanceof ParameterizedType) {
            Type[] p = ((ParameterizedType) t).getActualTypeArguments();
            this.clazz = (Class<T>) p[0];
        }
    }

    @Override
    public T findById(ID id)  {
        return baseDao.get(id);
    }

    @Override
    public List<T> query() {
        return baseDao.query();
    }

    @Override
    public List<T> queryPagedListByDetachedCriteria(DetachedCriteria detachedCriteria, Pagination pagination) {
        return baseDao.queryPagedListByDetachedCriteria(detachedCriteria, pagination);
    }

    @Override
    public List<T> queryByDetachedCriteria(DetachedCriteria detachedCriteria) {
        return baseDao.queryByDetachedCriteria(detachedCriteria);
    }

    @Override
    public void removeById(ID id) {
        baseDao.removeById(id);
    }

    @Override
    public void remove(T entity) {
        baseDao.remove(entity);
    }

    @Override
    public void save(T entity) {
        baseDao.save(entity);
    }

    @Override
    public void saveOrUpdate(T entity) {
        baseDao.saveOrUpdate(entity);
    }

    @Override
    public void merge(T entity) {
        baseDao.merge(entity);
    }
}
